import { defineStore } from 'pinia'
import { ref } from "vue";
import { analyzeNetworkData } from '@/utils/network-utils';
import { readFile2Object } from '@/utils/file-utils'

export const useNetworkStore = defineStore("networkStore", () => {
  // 绑定用，显示文件名
  const networkFiles = ref([]);
  // 用于读取的 File 数据
  const originNetworks = ref([]);
  // 真实的网络文件数据
  const realNetworks = ref([]);
  // 用于 Network 页面展示的数据
  const shownNetworks = ref([]);

  function addNetworkFile(file) {
    originNetworks.value.push(file)
  }

  async function build() {
    for (let index = 0; index < originNetworks.value.length; index++) {
      const file = originNetworks.value[index];
      const networkData = await readFile2Object(file);
      realNetworks.value = networkData.flat()
    }
    initNetworkMaps();
  }

  function initNetworkMaps() {
    shownNetworks.value = analyzeNetworkData(realNetworks.value);
    console.log('networkMaps', shownNetworks.value);
  }

  function clear() {
    networkFiles.value = [];
    realNetworks.value = [];
    originNetworks.value = [];
    shownNetworks.value = [];
  }

  return {
    networkFiles,
    shownNetworks,
    addNetworkFile,
    build,
    clear
  };
})